//
//  PopoverAnimationor.swift
//  CJPWB
//
//  Created by 陈锦彭 on 16/3/14.
//  Copyright © 2016年 chenjinpeng. All rights reserved.
//

import UIKit

class PopoverAnimationor: NSObject {
    ///判断popovaerview有没打开
    var isPresent : Bool = false
    lazy var presentedFrame : CGRect = CGRectZero
    var animateCallBack : ((isPresent : Bool) -> ())?

}


// MARK: - UIViewControllerTransitioningDelegate代理方法的实现
extension PopoverAnimationor : UIViewControllerTransitioningDelegate {
    
    ///改变弹出view和添加蒙版
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        let presentView = CJPPresentationController(presentedViewController: presented, presentingViewController: presenting)
        presentView.presentedFrame = presentedFrame
        
        return presentView
    }
    
    //弹出动画交给谁管理
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        if animateCallBack != nil {
            animateCallBack!(isPresent : isPresent)
        }
        return self
    }
    
    //消失的动画交给谁管理
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        if animateCallBack != nil {
            animateCallBack!(isPresent : isPresent)
        }
        return self
    }
    
}

// MARK: - UIViewControllerAnimatedTransitioning的代理方法
extension PopoverAnimationor : UIViewControllerAnimatedTransitioning {
    //返回动画时间间隔
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    
    //在该方法中有一个参数transitionContext, 该参数可以获取弹出的view和消失的view
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        // UITransitionContextFromViewKey, and UITransitionContextToViewKey
        isPresent ? presentViewAnimation(transitionContext) : dismissViewAnimation(transitionContext)
    }
    
}


extension PopoverAnimationor {
    ///弹出动画
    private func presentViewAnimation(transitionContext: UIViewControllerContextTransitioning) {
        let presentView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            //2.将弹出的view添加到容器视图中
            transitionContext.containerView()?.addSubview(presentView)
            
            //3.转场动画的实现
            presentView.transform = CGAffineTransformMakeScale(1.0, 0.0)
            presentView.layer.anchorPoint = CGPoint(x: 1.0, y: 0.001)
            UIView .animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                presentView.transform = CGAffineTransformIdentity
                }) { (_) -> Void in
                    //动画结束一定要告诉上下文动画已经结束
                    transitionContext.completeTransition(true)
            }
        }
    
    ///消失动画
    private func dismissViewAnimation(transitionContext: UIViewControllerContextTransitioning) {
        //获取消失的视图
        let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        UIView .animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            dismissView.transform = CGAffineTransformMakeScale(1.0, 0.001)
            }, completion: { (_) -> Void in
                //移除转场动画
                dismissView.removeFromSuperview()
                //动画结束一定要告诉上下文动画已经结束
                transitionContext.completeTransition(true)
        })
     }
}